package com.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class TestDemo {
	int i = 10;

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		// annotation();
		String s = "";
		TestDemo tt = (TestDemo) Class.forName("com.annotation.TestDemo")
				.newInstance();
		tt.i = 20;
		int i = tt.i;
		System.out.println(i);
		TestDemo tt2 = TestDemo.class.newInstance();
		System.out.println(tt2.i);
	}

	private static void annotation() {
		try {
			for (Method method : TestDemo.class.getClassLoader()
					.loadClass(("com.annotation.AnnotationExample"))
					.getMethods()) {
				// checks if MethodInfo annotation is present for the method
				if (method.isAnnotationPresent(MethodInfo.class)) {
					try {
						// iterates all the annotations available in the method
						for (Annotation anno : method.getDeclaredAnnotations()) {
							System.out.println("Annotation in Method " + method
									+ " : " + anno);
						}
						MethodInfo methodAnno = method
								.getAnnotation(MethodInfo.class);
						if (methodAnno.revision() == 1) {
							System.out.println("Method with revision no 1 = "
									+ method);
						}

					} catch (Throwable ex) {
						ex.printStackTrace();
					}
				}
			}
		} catch (SecurityException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
